<?php
namespace Api\Controller;

use Vendor;

class TraceController extends ApiController
{

    /**
     * 物流详情
     * 
     * @param
     *            int order_id
     * @param
     *            int checkup_id
     */
    public function traceDetail()
    {
        $order_id = I('post.order_id');
        $checkup_id = I('post.checkup_id');
        
        if(empty($order_id) && empty($checkup_id)) { response(- 1, '', '参数错误');}
        
        if ($order_id) {
            ! preg_match("/^[1-9]?\d*$/", $order_id) && response(- 1, '', '参数格式错误');
        }
        
        if ($checkup_id) {
            ! preg_match("/^[0-9]?\d*$/", $checkup_id) && response(- 1, '', '参数格式错误');
        }
        
        $result = array();
        $M_picture=M('picture');
        
        if ($order_id) {
            $M_order = M('Order');
            
            $M_order_shipping = M('order_shipping');
            
            $info = $M_order->find($order_id);
            ! $info && $this->error('非法的参数编号');
            
            $seller_shipping = $M_order_shipping->where(array(
                'order_id' => $order_id,
                'type' => 'seller'
            ))->find();
            $admin_shipping = $M_order_shipping->where(array(
                'order_id' => $order_id,
                'type' => 'admin'
            ))->find();
            
            $shipping_images=$M_picture->where(array('id'
                =>array('in',$seller_shipping['cover_ids'])))->select();
            foreach($shipping_images as $item){
                $seller_shipping['images'][]=get_url($item['id']);
            }
            
            $shipping_images=$M_picture->where(array('id'
                =>array('in',$admin_shipping['cover_ids'])))->select();
            foreach($shipping_images as $item){
                $admin_shipping['images'][]=get_url($item['id']);
            }
            
            
            $seller_shipping['shipping'] = getExpress($seller_shipping['shipping_code'], $seller_shipping['code']);
            $admin_shipping['shipping'] = getExpress($admin_shipping['shipping_code'], $admin_shipping['code']);
            
            $seller_shipping['shipping_title'] = get_table_field($seller_shipping['shipping_code'], 'code', 'title', 'shipping');
            $admin_shipping['shipping_title'] = get_table_field($admin_shipping['shipping_code'], 'code', 'title', 'shipping');
          

            $seller_shipping['shipping_mobile'] = get_table_field($seller_shipping['shipping_code'], 'code', 'mobile', 'shipping');
            $admin_shipping['shipping_mobile'] = get_table_field($admin_shipping['shipping_code'], 'code', 'mobile', 'shipping');
             
            $result['seller_shipping'] = $seller_shipping;
            $result['admin_shipping'] = $admin_shipping;
        } else 
            if ($checkup_id) {
                $M_checkup = M('checkup');
                
                $M_checkup_shipping = M('checkup_shipping');
                
                $info = $M_checkup->find($checkup_id);
                ! $info && $this->error('非法的参数编号');
                
                $seller_shipping = $M_checkup_shipping->where(array(
                    'checkup_id' => $checkup_id,
                    'type' => 'seller'
                ))->find();
                $admin_shipping = $M_checkup_shipping->where(array(
                    'checkup_id' => $checkup_id,
                    'type' => 'admin'
                ))->find();
                
                $shipping_images=$M_picture->where(array('id'
                    =>array('in',$seller_shipping['cover_ids'])))->select();
                foreach($shipping_images as $item){
                    $seller_shipping['images'][]=get_url($item['id']);
                }
                $shipping_images=$M_picture->where(array('id'
                    =>array('in',$admin_shipping['cover_ids'])))->select();
                foreach($shipping_images as $item){
                    $admin_shipping['images'][]=get_url($item['id']);
                }
                
                
                $seller_shipping['shipping'] = getExpress($seller_shipping['shipping_code'], $seller_shipping['code']);
                $admin_shipping['shipping'] = getExpress($admin_shipping['shipping_code'], $admin_shipping['code']);
                
                $seller_shipping['shipping_title'] = get_table_field($seller_shipping['shipping_code'], 'code', 'title', 'shipping');
                $admin_shipping['shipping_title'] = get_table_field($admin_shipping['shipping_code'], 'code', 'title', 'shipping');
               
                $seller_shipping['shipping_mobile'] = get_table_field($seller_shipping['shipping_code'], 'code', 'mobile', 'shipping');
                $admin_shipping['shipping_mobile'] = get_table_field($admin_shipping['shipping_code'], 'code', 'mobile', 'shipping');
               
                $result['seller_shipping'] = $seller_shipping;
                $result['admin_shipping'] = $admin_shipping;
            }
        response(1, $result);
    }
}

?>